package com.example.neutonote2.viewmodel;

import android.app.Application;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;

import com.example.neutonote2.data.model.Note;
import com.example.neutonote2.data.model.NoteWithTags;
import com.example.neutonote2.data.model.Tag;
import com.example.neutonote2.data.repository.NoteRepository;

import java.util.ArrayList;
import java.util.List;

/**
 * 笔记的ViewModel，用于管理UI相关的数据
 */
public class NoteViewModel extends AndroidViewModel {
    private final NoteRepository repository;
    private final LiveData<List<Note>> allNotes;
    private final LiveData<List<NoteWithTags>> allNotesWithTags;
    private final MutableLiveData<String> searchQuery = new MutableLiveData<>("");
    private final MutableLiveData<String> currentCategory = new MutableLiveData<>("全部");
    private LiveData<List<Note>> filteredNotes;
    private MutableLiveData<List<NoteWithTags>> searchResultsWithTags = new MutableLiveData<>();

    public NoteViewModel(Application application) {
        super(application);
        repository = new NoteRepository(application);
        allNotes = repository.getAllNotes();
        allNotesWithTags = repository.getAllNotesWithTags();
        filteredNotes = allNotes;
    }

    // 获取所有笔记
    public LiveData<List<Note>> getAllNotes() {
        return allNotes;
    }
    
    // 获取所有带标签的笔记
    public LiveData<List<NoteWithTags>> getAllNotesWithTags() {
        return allNotesWithTags;
    }

    // 按ID获取笔记
    public LiveData<Note> getNoteById(int id) {
        return repository.getNoteById(id);
    }
    
    // 按ID获取带标签的笔记
    public LiveData<NoteWithTags> getNoteWithTags(int id) {
        return repository.getNoteWithTags(id);
    }

    // 按分类筛选笔记
    public LiveData<List<Note>> getNotesByCategory(String category) {
        currentCategory.setValue(category);
        filteredNotes = repository.getNotesByCategory(category);
        return filteredNotes;
    }
    
    // 按分类筛选带标签的笔记
    public LiveData<List<NoteWithTags>> getNotesWithTagsByCategory(String category) {
        return repository.getNotesWithTagsByCategory(category);
    }

    // 搜索笔记
    public LiveData<List<Note>> searchNotes(String query) {
        searchQuery.setValue(query);
        filteredNotes = repository.searchNotes(query);
        return filteredNotes;
    }
    
    // 搜索带标签的笔记
    public LiveData<List<NoteWithTags>> searchNotesWithTags(String query) {
        // 使用Transformations将普通笔记搜索结果转换为带标签的笔记
        searchQuery.setValue(query);
        
        // 首先获取普通搜索结果
        LiveData<List<Note>> searchResults = repository.searchNotes(query);
        
        // 然后使用Transformations转换为带标签的笔记
        return Transformations.switchMap(searchResults, notes -> {
            if (notes == null || notes.isEmpty()) {
                searchResultsWithTags.setValue(new ArrayList<>());
                return searchResultsWithTags;
            }
            
            // 为每个笔记ID获取带标签的信息
            List<Integer> noteIds = new ArrayList<>();
            for (Note note : notes) {
                noteIds.add(note.getId());
            }
            
            // 获取这些笔记的完整信息（包括标签）
            return repository.getNotesWithTagsByIds(noteIds);
        });
    }
    
    // 获取需要复习的笔记
    public LiveData<List<Note>> getNotesToReview() {
        return repository.getNotesToReview();
    }

    // 获取当前分类
    public MutableLiveData<String> getCurrentCategory() {
        return currentCategory;
    }

    // 获取搜索关键词
    public MutableLiveData<String> getSearchQuery() {
        return searchQuery;
    }

    // 获取筛选后的笔记
    public LiveData<List<Note>> getFilteredNotes() {
        return filteredNotes;
    }

    // 插入笔记
    public void insert(Note note) {
        repository.insert(note);
    }
    
    // 插入笔记并关联标签
    public void insertNoteWithTags(Note note, List<Tag> tags) {
        repository.insertNoteWithTags(note, tags);
    }

    // 更新笔记
    public void update(Note note) {
        repository.update(note);
    }
    
    // 更新笔记和标签
    public void updateNoteWithTags(Note note, List<Tag> tags) {
        repository.updateNoteWithTags(note, tags);
    }

    // 删除笔记
    public void delete(Note note) {
        repository.delete(note);
    }
    
    // 按ID删除笔记
    public void deleteById(int id) {
        repository.deleteById(id);
    }
} 